<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm }">
        <el-form-item label="风险分析对象">
          <el-input v-model="filterForm.riskAnalysisName" />
        </el-form-item>
        <el-form-item label="重大危险源级别">
          <el-input v-model="filterForm.dangerLevel" />
        </el-form-item>
      </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="onBackClick">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sysRiskAnalysis:create']"
          >新增</el-button
        >
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #beforemeasuresProject>
        <el-table-column
          label="措施分类"
          align="center"
          key="measuresType"
          prop="measuresType"
        />
        <el-table-column
          label="措施子类"
          align="center"
          key="measuresTypeSon"
          prop="measuresTypeSon"
        />
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click.stop="handleEdit(row)"
          v-hasPermi="['sysRiskAnalysis:update']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click.stop="handleDel(row)"
          v-hasPermi="['sysRiskAnalysis:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      :slots="['defaultSlot']"
      @onSubmit="onSubmit"
      :title="dialogType == 'add' ? '新增风险排查' : '修改风险排查'"
    >
      <template #defaultSlot>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="措施类别" label-position="right">
              <el-select
                @change="changeMeasuresOne"
                style="width: 100%"
                v-model="form.measuresType"
              >
                <el-option value="工程技术"></el-option>
                <el-option value="维护保养"></el-option>
                <el-option value="操作行为"></el-option>
                <el-option value="应急措施"></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="措施子类" label-position="right">
              <el-select
                style="width: 100%"
                @change="userIdChange"
                v-model="form.measuresTypeSon"
                :disabled="!form.measuresType"
              >
                <el-option
                  v-for="cs in cs2"
                  :value="cs"
                  :label="cs"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <!-- <el-col :span="12"> -->
          <!-- <el-form-item label="项目" prop="userId" label-position="right">
              <el-input v-model="form.measuresProject"> </el-input>
            </el-form-item>
          </el-col> -->
        </el-row>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { fxpc as APIS } from "@/api/hdanger/fxfxqd/index.js";
import formJson from "./fxpc.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { selectOptions } from "./consts";

const emit = defineEmits("onBackClick");
const { proxy } = getCurrentInstance();
const props = defineProps({
  parentNode: {
    type: Object,
    default: () => ({}),
  },
});
const cs2 = ref([]);
const changeMeasuresOne = (val) => {
  cs2.value = selectOptions[val];
};
const onBackClick = () => {
  emit("onBackClick");
};

const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "cycle",
      "workShop",
      "equipmentType",
      "securityEquipmentName",
      "person",
    ].includes(i.prop)
  ),
});
const plugAdd = () => {
  form.value.securityRiskId = props.parentNode.id;
};
const plugEdit = (row) => {
  form.value.measuresTypeSon = row.measuresTypeSon;
  form.value.measuresType = row.measuresType;
};
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugAdd,
  plugEdit,
});
watch(
  () => props.parentNode,
  (val) => {
    console.log(val);
    queryParams.value.securityRiskId = val.id || null;
    getList();
  },
  {
    deep: true,
  }
);
</script>

<style lang="scss" scoped></style>
